<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>医疗AI功能测试</title>
    <style>
        body {
            font-family: 'Microsoft YaHei', sans-serif;
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }

        .container {
            background: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }

        h1 {
            color: #2c3e50;
            text-align: center;
            margin-bottom: 30px;
        }

        .test-section {
            margin-bottom: 30px;
            padding: 20px;
            border: 1px solid #ddd;
            border-radius: 8px;
            background-color: #fafafa;
        }

        .test-section h3 {
            color: #34495e;
            margin-bottom: 15px;
        }

        textarea,
        input[type="text"] {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            margin-bottom: 10px;
            font-family: inherit;
        }

        textarea {
            min-height: 100px;
            resize: vertical;
        }

        button {
            background-color: #3498db;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
        }

        button:hover {
            background-color: #2980b9;
        }

        .result {
            margin-top: 15px;
            padding: 15px;
            background-color: #ecf0f1;
            border-radius: 4px;
            white-space: pre-wrap;
            font-family: 'Courier New', monospace;
            max-height: 300px;
            overflow-y: auto;
        }

        .loading {
            color: #7f8c8d;
            font-style: italic;
        }

        .error {
            color: #e74c3c;
            background-color: #fdf2f2;
            border: 1px solid #f5c6cb;
        }

        .success {
            color: #27ae60;
            background-color: #f0f9f0;
            border: 1px solid #c3e6c3;
        }
    </style>
</head>

<body>
    <div class="container">
        <h1>🏥 医疗AI功能测试平台</h1>

        <div class="test-section">
            <h3>🔗 AI服务连接测试</h3>
            <button onclick="testConnection()">测试连接</button>
            <div id="connectionResult" class="result" style="display: none;"></div>
        </div>

        <div class="test-section">
            <h3>💊 处方分析</h3>
            <textarea id="prescriptionText" placeholder="请输入处方内容...">阿莫西林胶囊 0.5g 口服 每日3次</textarea>
            <button onclick="analyzePrescription()">分析处方</button>
            <div id="prescriptionResult" class="result" style="display: none;"></div>
        </div>

        <div class="test-section">
            <h3>🔍 诊断建议</h3>
            <input type="text" id="symptoms" placeholder="请输入症状描述..." value="头痛、发热、咳嗽">
            <button onclick="getDiagnosticSuggestion()">获取诊断建议</button>
            <div id="diagnosticResult" class="result" style="display: none;"></div>
        </div>
    </div>

    <script>
        const API_BASE = '/api/MedicalAI';

        async function makeRequest(endpoint, data = null) {
            try {
                const url = `${API_BASE}${endpoint}`;
                const options = {
                    method: data ? 'POST' : 'GET',
                    headers: { 'Content-Type': 'application/json' }
                };
                if (data) options.body = JSON.stringify(data);
                const response = await fetch(url, options);
                const result = await response.json();
                return result.success ? { success: true, data: result.data } : { success: false, error: result.message };
            } catch (error) {
                return { success: false, error: '网络请求失败: ' + error.message };
            }
        }

        function showResult(elementId, content, isError = false) {
            const element = document.getElementById(elementId);
            element.style.display = 'block';
            element.textContent = content;
            element.className = 'result ' + (isError ? 'error' : 'success');
        }

        async function testConnection() {
            const resultDiv = document.getElementById('connectionResult');
            resultDiv.style.display = 'block';
            resultDiv.textContent = '正在测试连接...';
            resultDiv.className = 'result loading';
            const result = await makeRequest('/test-connection');
            if (result.success) {
                showResult('connectionResult', '✅ ' + result.data);
            } else {
                showResult('connectionResult', '❌ ' + result.error, true);
            }
        }

        async function analyzePrescription() {
            const prescriptionText = document.getElementById('prescriptionText').value;
            if (!prescriptionText.trim()) { alert('请输入处方内容'); return; }
            const resultDiv = document.getElementById('prescriptionResult');
            resultDiv.style.display = 'block';
            resultDiv.textContent = '正在分析处方...';
            resultDiv.className = 'result loading';
            const result = await makeRequest('/analyze-prescription', { prescriptionText: prescriptionText });
            if (result.success) {
                showResult('prescriptionResult', result.data);
            } else {
                showResult('prescriptionResult', '❌ ' + result.error, true);
            }
        }

        async function getDiagnosticSuggestion() {
            const symptoms = document.getElementById('symptoms').value;
            if (!symptoms.trim()) { alert('请输入症状描述'); return; }
            const resultDiv = document.getElementById('diagnosticResult');
            resultDiv.style.display = 'block';
            resultDiv.textContent = '正在获取诊断建议...';
            resultDiv.className = 'result loading';
            const result = await makeRequest('/diagnostic-suggestion', { symptoms: symptoms });
            if (result.success) {
                showResult('diagnosticResult', result.data);
            } else {
                showResult('diagnosticResult', '❌ ' + result.error, true);
            }
        }

        window.onload = function () { testConnection(); };
    </script>
</body>

</html>